<?php

/**
 * Description of RepoConfiguracion
 *
 * @author  Mariano Renkine
 * @author  Fernando Arau
 */
class RepoConfiguracion extends Repositorio
{

  function __construct()
  {
    $this->tabla = "configuracion";
    $this->id = "nombre";
  }

  function agregar(Configuracion $configuracion)
  {
    try
    {

      $link = $this->OpenDB();
      $this->beginTransaction();
      $query = "INSERT INTO " . $this->tabla . "(nombre, valor) values (";
      $query .= "'" . $configuracion->getNombre() . "', ";
      $query .= "'" . $configuracion->getValor() . "')";
      $result = $this->PostSQL($query);
      if ($result)
      {
        $this->commitTransaction();
      } else
      {
        $this->rollbackTransaction();
      }
      $this->CloseDB($link);
      return $result;
    } catch (Exception $ex)
    {
      return $ex;
    }
  }

  function modificar(Configuracion $configuracion)
  {
    try
    {
      $link = $this->OpenDB();
      $this->beginTransaction();
      $query = "UPDATE " . $this->tabla . " SET ";
      $query .= "nombre = '" . $configuracion->getNombre() . "', ";
      $query .= "valor = '" . $configuracion->getValor() . "' ";
      $query .= " WHERE " . $this->id . " = '" . $configuracion->getNombre()."'";
      $result = $this->PostSQL($query);
      $this->commitTransaction();
      $this->CloseDB($link);
      return $result;
    } catch (Exception $ex)
    {
      return $ex->getCode() + "|" + $ex->getMessage();
    }
  }

  function buscar($nombre)
  {
    $link = $this->OpenDB();
    $queryTxt = "SELECT  valor FROM " . $this->tabla . " WHERE " . $this->id . " = '" . $nombre."'";
    $query = $this->GetSQLFirstValue($queryTxt);
    $this->CloseDB($link);
    $configuracion = new Configuracion();
    if ($query)
    {
      $configuracion->setValor($query);
      $configuracion->setNombre($nombre);
    }
    return $configuracion;
  }  
}

?>
